package com.docmgmt.mvp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.docmgmt.mvp.entity.PermissionEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 权限Mapper
 */
@Mapper
public interface PermissionMapper extends BaseMapper<PermissionEntity> {

    /**
     * 查询角色的所有权限
     * sys_permission表不使用软删除（无is_deleted字段）
     */
    @Select("SELECT p.* FROM sys_permission p " +
            "INNER JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId}")
    List<PermissionEntity> selectPermissionsByRoleId(@Param("roleId") Long roleId);

    /**
     * 查询用户的直接权限（不通过角色）
     * sys_permission表不使用软删除（无is_deleted字段）
     */
    @Select("SELECT p.* FROM sys_permission p " +
            "INNER JOIN sys_user_permission up ON p.permission_code = up.permission_code " +
            "WHERE up.user_id = #{userId}")
    List<PermissionEntity> selectDirectPermissionsByUserId(@Param("userId") Long userId);

    /**
     * 根据权限代码查询权限
     * sys_permission表不使用软删除（无is_deleted字段）
     */
    @Select("SELECT * FROM sys_permission WHERE permission_code = #{permissionCode}")
    PermissionEntity selectByPermissionCode(@Param("permissionCode") String permissionCode);
}
